<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>十字相乘计算器</title>
    <style>
        body {
            width: 50% ;
            margin-left: auto ;
            margin-top: 30px ;
            margin-right: auto ;
            margin-bottom: 30px ;
        }
        footer {
            color: gray;
            position:absolute;
            bottom: 0px;
        }
    </style>
    <script src="http://cdnjs.cloudflare.com/ajax/libs/jquery/1.9.1/jquery.min.js">
    </script>
    <script>
        function run(){
            var a=$('input#a').val();
            var b=$('input#b').val();
            var c=$('input#c').val();
            var result=Factorise(a,b,c);
            if (result[0]===false){
                var contence="不能分解因式"
            }
            if (result[0]===true){
                result=result[1];
                var contence="可以分解因式"+"("+result[0][0]+"x+("+result[0][1]+"))*("+result[1][0]+"x+("+result[1][1]+"))"
            }
            let formate="<div>"+contence+"</div>";
            $("#结果").html(formate);
        }
        function Factorise(a,b,c){
            a=Number(a);
            b=Number(b);
            c=Number(c);
            if (isNaN(a)||isNaN(b)||isNaN(c)){
                return [false]
            }
            var factor_a=findFactor(a);
            var factor_c=findFactor(c);
            if ((b**2-a*c)===0){
                return [false]
            }
            var numbers_a;
            var numbers_c;
            for (numbers_a of factor_a){
                for (numbers_c of factor_c){
                    if ((numbers_a[0]*numbers_c[0]+numbers_a[1]*numbers_c[1])===b){
                        return [true,[[numbers_a[0],numbers_c[1]],[numbers_a[1],numbers_c[0]]]]
                    }
                    if ((numbers_a[1]*numbers_c[0]+numbers_a[0]*numbers_c[1])===b){
                        return [true,[[numbers_a[1],numbers_c[1]],[numbers_a[0],numbers_c[0]]]]
                    }
                }
            }
            return [false]
        }
        function findFactor(num){
            var factors=[];
            var i;
            for(i=Math.abs(num);i>=-Math.abs(num);i--){
                if ((num%i)===0){
                    var theOther=num/i;
                    factors.push([i,theOther]);
                }
            }
            return factors
        }
    </script>
</head>
<body>
    <article>
    <h1>十字相乘计算器</h1>
    <p>ax²±bx±c => (dx+e)(fx+g)</p>
    <p>a:<input id=a type="text" name=a></p>
    <p>b:<input id=b type="text" name=b></p>
    <p>c:<input id=c type="text" name=c></p>
    <p></p>
    <button onclick=run()>确定</button>
    <div id="结果"></div>
    </article>
    <footer>Copyright 2020 Hengyuan Zhao</footer>
</body>
</html>